#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using starLiGHT.GUI.Widgets;
#endregion

namespace starLiGHT.GUI
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        #region Private Members
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GuiManager guiManager;

        int frameRate = 0;
        int frameCounter = 0;
        TimeSpan elapsedTime = TimeSpan.Zero;

        ToolStripLabel fpsToolStripLabel;
        #endregion

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;

            this.IsFixedTimeStep = false;
            graphics.SynchronizeWithVerticalRetrace = false;

            //graphics.IsFullScreen = true;
        }

        protected override void Initialize()
        {
            GuiManagerComponent guiManagerComponent = new GuiManagerComponent(this, @"Content/Skins/DarkSkin.xml");
            this.guiManager = guiManagerComponent.GuiManager;
            this.guiManager.SpriteCursorEnabled = true;
            Components.Add(guiManagerComponent);

            base.Initialize();
        }

        public void button_click(object sender, EventArgs e)
        {
#if !XNA40
            if (GraphicsDevice.RenderState.FillMode == FillMode.Solid)
            {
                GraphicsDevice.RenderState.FillMode = FillMode.WireFrame;
            }
            else
            {
                GraphicsDevice.RenderState.FillMode = FillMode.Solid;
            }

            ((Button)sender).Text = GraphicsDevice.RenderState.FillMode == FillMode.Solid ? "Solid" : "WireFrame";
#else
            if (GraphicsDevice.RasterizerState.FillMode == FillMode.Solid)
            {
                GraphicsDevice.RasterizerState = new RasterizerState() { FillMode = FillMode.WireFrame };
            }
            else
            {
                GraphicsDevice.RasterizerState = new RasterizerState() { FillMode = FillMode.Solid };
            }

            ((Button)sender).Text = GraphicsDevice.RasterizerState.FillMode == FillMode.Solid ? "Solid" : "WireFrame";
#endif
         }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            #region MenuStrip & StatusStrip
            
            MenuStrip menuStrip = new MenuStrip(guiManager, "MainMenu00");
            menuStrip.Location = new Vector2(0f, 0f);
            menuStrip.Size = new Vector2(1280f, 23f);

            ToolStripMenuItem tsmi = new ToolStripMenuItem();
            tsmi.Text = "File";
            menuStrip.Items.Add(tsmi);

            ToolStripMenuItem exitMenuItem = new ToolStripMenuItem();
            exitMenuItem.Text = "Exit";
            exitMenuItem.Click += new EventHandler(exitMenuItem_Click);
            tsmi.DropDownItems.Add(exitMenuItem);

            tsmi = new ToolStripMenuItem();
            tsmi.Text = "Demo-Windows";
            menuStrip.Items.Add(tsmi);

            ToolStripMenuItem subMenu = new ToolStripMenuItem();
            subMenu.Text = "Window with simple controls";
            subMenu.Click += new EventHandler(subMenu_SimpleControls_Click);
            tsmi.DropDownItems.Add(subMenu);

            subMenu = new ToolStripMenuItem();
            subMenu.Text = "Scrollbar Test";
            subMenu.Click += new EventHandler(subMenu_ScrollbarTest_Click);
            tsmi.DropDownItems.Add(subMenu);

            subMenu = new ToolStripMenuItem();
            subMenu.Text = "Scissor Test";
            subMenu.Click += new EventHandler(subMenu_ScissorTest_Click);
            tsmi.DropDownItems.Add(subMenu);

            subMenu = new ToolStripMenuItem();
            subMenu.Text = "TableLayoutPanel Test";
            subMenu.Click += new EventHandler(subMenu_TableLayoutTest_Click);
            tsmi.DropDownItems.Add(subMenu);

            subMenu = new ToolStripMenuItem();
            subMenu.Text = "Scrolling Test";
            subMenu.Click += new EventHandler(subMenu_ScrollingTest_Click);
            tsmi.DropDownItems.Add(subMenu);

            tsmi = new ToolStripMenuItem();
            tsmi.Text = "Debug";
            menuStrip.Items.Add(tsmi);

            subMenu = new ToolStripMenuItem();
            subMenu.Text = "Toggle Fullscreen";
            tsmi.DropDownItems.Add(subMenu);

            subMenu = new ToolStripMenuItem();
            subMenu.Text = "Wireframe";
            tsmi.DropDownItems.Add(subMenu);

            subMenu = new ToolStripMenuItem();
            subMenu.Text = "Show Bounding Rectangles";
            tsmi.DropDownItems.Add(subMenu);

            tsmi = new ToolStripMenuItem();
            tsmi.Text = "Help";
            menuStrip.Items.Add(tsmi);

            guiManager.Widgets.Add(menuStrip);

            StatusStrip statusStrip = new StatusStrip(guiManager, "StatusStrip00");
            statusStrip.Size = new Vector2(1280f, 23f);
            statusStrip.Location = new Vector2(0f, 720 - statusStrip.Size.Y);

            fpsToolStripLabel = new ToolStripLabel();
            fpsToolStripLabel.Text = "FPS: n/a";
            statusStrip.Items.Add(fpsToolStripLabel);

            guiManager.Widgets.Add(statusStrip);
            
            #endregion

            //#region "TopLevel Control"

            //Button btn2 = new Button(guiManager, "Button02");
            //btn2.Text = "Test Button";
            //btn2.Location = new Vector2(170, 30);
            //btn2.Size = new Vector2(150, 20);
            //guiManager.Widgets.Add(btn2);

            //TextBox tb = new TextBox(guiManager, "TextBox01");
            //tb.Location = new Vector2(20, 30);
            //tb.Size = new Vector2(100, 20);
            //guiManager.Widgets.Add(tb);

            //#endregion

#if !XNA40
            guiManager.LoadGUI(@"GUI/Test01");
#endif
        }

        void subMenu_ScissorTest_Click(object sender, EventArgs e)
        {
            Window w = guiManager.CreateWindow("TestWindow03", "Scissor test");
            w.Location = new Vector2(300, 300);
            w.Focused = true;

            Label label = new Label(guiManager, "Label00");
            label.BackColor = Color.White;
            label.Text = "This is a long text which should be scissored... Right here >>>>> | This text should be invisble :-)";
            label.Location = new Vector2(170, 0);
            label.Size = new Vector2(150, 20);
            w.Widgets.Add(label);
        }

        void subMenu_TableLayoutTest_Click(object sender, EventArgs e)
        {
            Window w = guiManager.CreateWindow("TestWindow05", "TableLayoutPanel test");
            w.Location = new Vector2(300, 300);
            w.Size = new Vector2(256 + 64, 256 + 64);
            w.Focused = true;

            TableLayoutPanel table = new TableLayoutPanel(guiManager, "Table", 3,
                150, 10, 150,
                20,
                5,
                20,
                5,
                20);
            table.Location = new Vector2(50, 100);
            guiManager.Widgets.Add(table);

            for (int i = 0; i < 4; i += 2)
            {
                for (int j = 0; j < 5; j += 2)
                {
                    Button tableBtn = new Button(guiManager, string.Format("table_button_{0}_{1}", i, j));
                    tableBtn.Text = string.Format("Table Button [{0}, {1}]", i, j);
                    table[i, j] = tableBtn;
                }
            }
        }

        void subMenu_ScrollingTest_Click(object sender, EventArgs e)
        {
            Window w = guiManager.CreateWindow("TestWindow04", "Scrolling test");
            w.Location = new Vector2(300, 300);
            w.Size = new Vector2(256 + 64, 256 + 64);
            w.Focused = true;

            TextureBox texBox = new TextureBox(guiManager, "TextureBox01");
            texBox.Texture = Content.Load<Texture2D>("Textures/test_checkerboard");
            texBox.Location = new Vector2(0, 0);
            texBox.Size = new Vector2(128, 128);
            w.Widgets.Add(texBox);

            texBox = new TextureBox(guiManager, "TextureBox02");
            texBox.Texture = Content.Load<Texture2D>("Textures/test_checkerboard");
            texBox.Location = new Vector2(256, 0);
            texBox.Size = new Vector2(128, 128);
            w.Widgets.Add(texBox);

            texBox = new TextureBox(guiManager, "TextureBox03");
            texBox.Texture = Content.Load<Texture2D>("Textures/test_checkerboard");
            texBox.Location = new Vector2(0, 256);
            texBox.Size = new Vector2(128, 128);
            w.Widgets.Add(texBox);

            texBox = new TextureBox(guiManager, "TextureBox04");
            texBox.Texture = Content.Load<Texture2D>("Textures/test_checkerboard");
            texBox.Location = new Vector2(256, 256);
            texBox.Size = new Vector2(128, 128);
            w.Widgets.Add(texBox);
        }

        void subMenu_ScrollbarTest_Click(object sender, EventArgs e)
        {
            Window w = guiManager.CreateWindow("TestWindow02", "Scrollbar test");
            w.Location = new Vector2(200, 200);
            w.Focused = true;

            HScrollBar hscroll = new HScrollBar(guiManager, "ScrollBar01");
            hscroll.Location = new Vector2(10, 10);
            hscroll.Size = new Vector2(400, 15);
            hscroll.Value = 0;
            w.Widgets.Add(hscroll);

            VScrollBar vscroll = new VScrollBar(guiManager, "ScrollBar02");
            vscroll.Location = new Vector2(410, 25);
            vscroll.Size = new Vector2(15, 150);
            w.Widgets.Add(vscroll);

            Panel p = new Panel(guiManager, "Panel01");
            p.Location = new Vector2(10, 26);
            p.Size = new Vector2(399, 149);
            w.Widgets.Add(p);
        }

        void subMenu_SimpleControls_Click(object sender, EventArgs e)
        {
            Window w = guiManager.CreateWindow("TestWindow01", "Window with simple controls");
            w.Location = new Vector2(100, 100);
            w.BackColor = Color.Olive;
            w.ZOrder = -0.1f;
            w.Focused = true;

            Button btn;
            btn = new Button(guiManager, "Button01");
            btn.Text = "WireFrame";
            btn.Location = new Vector2(170, 0);
            btn.Size = new Vector2(150, 20);
            btn.Click += new EventHandler(button_click);
            w.Widgets.Add(btn);

            btn = new Button(guiManager, "Button02");
            btn.Text = "Test Button";
            btn.Location = new Vector2(170, 30);
            btn.Size = new Vector2(150, 20);
            w.Widgets.Add(btn);

            Checkbox checkbox = new Checkbox(guiManager, "Checkbox01");
            checkbox.Location = new Vector2(170, 60);
            w.Widgets.Add(checkbox);

            for (int i = 0; i < 5; i++)
            {
                RadioButton rb = new RadioButton(guiManager, "RadioButton" + i.ToString());
                rb.Location = new Vector2(250, 60 + i * 30);
                w.Widgets.Add(rb);
            }

            TextBox tb2 = new TextBox(guiManager, "TextBox01");
            tb2.Location = new Vector2(100, 150);
            tb2.Size = new Vector2(100, 20);
            w.Widgets.Add(tb2);

            Label label;
            label = new Label(guiManager, "Label00");
            label.BackColor = Color.White;
            label.Text = "Label01";
            label.Size = new Vector2(150, 20);
            w.Widgets.Add(label);

            for (int i = 1; i < 9; i++)
            {
                label = new Label(guiManager, "Label0" + (i + 1).ToString());
                label.BackColor = Color.White;
                label.Text = "Label0" + (i + 1).ToString();
                label.Location = new Vector2(0, i * 25);
                label.Size = new Vector2(150, 20);
                w.Widgets.Add(label);
            }
        }

        void exitMenuItem_Click(object sender, EventArgs e)
        {
            this.Exit();
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime >= TimeSpan.FromSeconds(1))
            {
                elapsedTime -= TimeSpan.FromSeconds(1);
                frameRate = frameCounter;
                frameCounter = 0;

                fpsToolStripLabel.Text = string.Format("FPS: {0}", frameRate);
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            frameCounter++;

            GraphicsDevice.Clear(Color.DarkGray);

            base.Draw(gameTime);

            //spriteBatch.Begin();
            //MenuStrip ms = guiManager.Widgets[1] as MenuStrip;

            //if (ms != null)
            //{
            //    foreach (ToolStripMenuItem tsi in ms.Items)
            //    {
            //        RectangleF totalRect = RectangleF.Empty;

            //        totalRect = RectangleF.Union(totalRect, tsi.GetHitTestRectangle());
            //        foreach (ToolStripMenuItem subItem in tsi.DropDownItems)
            //        {
            //            totalRect = RectangleF.Union(totalRect, subItem.GetHitTestRectangle());
            //        }

            //        spriteBatch.DrawBox(totalRect, Color.Red);
            //    }
            //}
            //spriteBatch.End();
        }
    }
}
